﻿using IdentityModel.Client;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;

namespace TlToolBase;

/// <summary>
/// 帮助类
/// </summary>
public class HttpHelp
{
    /// <summary>
    /// 是否使用Token
    /// </summary>
    public bool IsUseToken { get; set; }
    /// <summary>
    /// 获取
    /// </summary>
    /// <param name="url"></param>
    public async void Get(string url, string token = "")
    {
        var httpClient = HttpCreate(token);
        var response = await httpClient.GetAsync(url);
        if (response.IsSuccessStatusCode)
        {
            var result = await response.Content.ReadAsStringAsync();
            Console.WriteLine($"响应内容： {result}");
        }
        else
        {
            Console.WriteLine($"请求失败，状态码： {response.StatusCode}");
        }
    }

    /// <summary>
    /// 提交
    /// </summary>
    /// <param name="url"></param>
    /// <param name="data"></param>
    public async Task<string> Post(string url, object data, string token = "")
    {
        var value = System.Text.Json.JsonSerializer.Serialize(data);
        var httpClient = HttpCreate(token);
        var content = new StringContent(value, Encoding.UTF8, "application/json");
        var response = await httpClient.PostAsync(url, content);

        if (response.IsSuccessStatusCode)
        {
            var result = await response.Content.ReadAsStringAsync();
            Console.WriteLine($"响应内容： {result}");
            return result;
        }
        else
        {
            Console.WriteLine($"请求失败，状态码： {response.StatusCode}");
            return response.ToString();
        }
    }
    /// <summary>
    /// 参数提交
    /// </summary>
    /// <param name="url"></param>
    /// <param name="data"></param>
    /// <param name="token"></param>
    /// <returns></returns>
    public async Task<string> PostParams(string url, List<Tuple<string,string>> data, string token = "")
    {
        string value = "";
        for (int i = 0; i < data.Count; i++)
        {
            value += string.Format("{0}={1}", data[i].Item1, data[i].Item2);
            if (i != data.Count - 1)
            {
                value += "&";
            }
        }
        var httpClient = HttpCreate(token);
        var response = await httpClient.PostAsync(url + "?" + value, null);
        if (response.IsSuccessStatusCode)
        {
            var result = await response.Content.ReadAsStringAsync();
            Console.WriteLine($"响应内容： {result}");
            return result;
        }
        else
        {
            Console.WriteLine($"请求失败，状态码： {response.StatusCode}");
            return response.ToString();
        }
    }
    /// <summary>
    /// http创建
    /// </summary>
    private HttpClient HttpCreate(string token)
    {
        var client = HttpClientFactory.Create();
        if (!string.IsNullOrEmpty(token))
        {
            client.DefaultRequestHeaders.Authorization = CreateBasicHeader(token);
        }
        return client;
    }
    /// <summary>
    /// 创建token请求头
    /// </summary>
    /// <returns></returns>
    public static AuthenticationHeaderValue CreateBasicHeader(string token)
    {
        return new AuthenticationHeaderValue("Bearer", token);
    }
}

